package geos

import (
	"fmt"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/enums/loc_enum"
	"strings"
)

func ToString(p []*Point, regionType int) *string {
	if regionType == loc_enum.LocRegionType.Line() {
		return ToLineString(p)
	}
	if regionType == loc_enum.LocRegionType.Polygon() {
		return ToPolygonString(p)
	}
	if regionType == loc_enum.LocRegionType.Circle() {
		return ToPointString(p[0])
	}
	return nil
}

func ToPointString(p *Point) *string {
	if p == nil {
		return nil
	}
	str := fmt.Sprintf("SRID=4326;POINT(%s)", PointString(p))
	return &str
}

func PointString(p *Point) string {
	return fmt.Sprintf("%.8f %.8f", p.Longitude, p.Latitude)
}

func ToLineString(ps []*Point) *string {
	psstr := PointsToString(ps)
	if psstr == nil {
		return nil
	}
	str := fmt.Sprintf("SRID=4326;LINESTRING(%s)", *psstr)
	return &str
}

func ToPolygonString(ps []*Point) *string {
	psstr := PointsToString(ps)
	if psstr == nil {
		return nil
	}
	str := fmt.Sprintf("SRID=4326;polygon((%s))", *psstr)
	return &str
}

func ToMultiLineString(ps [][]*Point) *string {
	if ps == nil || len(ps) == 0 {
		return nil
	}
	pstrArray := make([]string, 0)
	for _, p := range ps {
		pstr := PointsToString(p)
		if pstr == nil {
			return nil
		}
		pstrArray = append(pstrArray, fmt.Sprintf("(%s)", *pstr))
	}
	psstr := strings.Join(pstrArray, ",")

	str := fmt.Sprintf("SRID=4326;MULTILINESTRING(%s)", psstr)
	return &str
}

func PointsToString(ps []*Point) *string {
	if ps == nil || len(ps) == 0 {
		return nil
	}
	pstr := []string{}
	for _, p := range ps {
		pstr = append(pstr, PointString(p))
	}
	psstr := strings.Join(pstr, ",")
	return &psstr
}

// 转圆形字符串
func ToCircleString(p *Point, radius *float64) {

}
